﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Besture.Common  
{
    public class SortIDOperation
    {
        //BaseDataAccess DBClass = new BaseDataAccess();
        //int Max = 20;
        /// <summary>
        /// 获取表的最小的可用的排序ID号
        /// </summary>
        /// <param name="table">标签表名</param>
        /// <returns></returns>
        public string InitSortID(string table, string where)
        {
            int SortID = 0;

            DataTable dt = DbHelperSQL.Query("select top 1 SortID from " + table + " where " + where + " Order by sortid desc").Tables[0];
            if (dt.Rows.Count > 0)
            {
                SortID = int.Parse(dt.Rows[0]["sortid"].ToString()) + 1;
            }

            return SortID.ToString();
        }
        /// <summary>
        /// 插入数据时判断SortID
        /// </summary>
        /// <param name="table">插入表</param>
        /// <param name="AddPageSortID">系统初始化SortID</param>
        /// <param name="AddPageSortIDEnd">用户更改后的SortID</param>
        /// <returns></returns>
        public int AddSortID(string table, int AddPageSortID, int AddPageSortIDEnd, string where)
        {
           
                int InsertSortID = AddPageSortIDEnd;
                //判断用户是否更改了SortID
                //若没更改或将SortID变大.直接将系统自动生成的SORTID插入数据库
                if ((AddPageSortID == AddPageSortIDEnd) || AddPageSortID < AddPageSortIDEnd)
                {
                    InsertSortID = AddPageSortID;
                }
                //若将SortID变小,则表示要将数据的SORTID移前。则将大于SORTID的数据移后一位
                else if (AddPageSortID > AddPageSortIDEnd)
                {
                    if (string.IsNullOrEmpty(where))
                    {
                        where = " 1=1";
                    }
                    DbHelperSQL.ExecuteSql("UPDATE " + table + " SET SortId = SortId +1 where " + where + " and SortId >= " + AddPageSortIDEnd);

                    InsertSortID = AddPageSortIDEnd;
                }
              
                return InsertSortID;
         
        }
        /// <summary>
        /// 修改SortID
        /// </summary>
        /// <param name="Table">表名</param>
        /// <param name="SortIDBefore">修改之前的SortID</param>
        /// <param name="SortIDEnd">修改之后的SORTID</param>
        /// <returns>返回正确的更新值</returns>
        public int UpdateSortID(string Table, int SortIDBefore, int SortIDEnd, string where)
        {
           
                string sql = "";
                //如果将SORTID变小.
                if (SortIDBefore > SortIDEnd)
                {
                    if (string.IsNullOrEmpty(where))
                    {
                        where = " 1=1";
                    }
                    else
                    {
                        sql = "UPDATE " + Table + " SET SortId = SortId +1 where " + where + " and SORTID BETWEEN " + SortIDEnd + " AND " + SortIDBefore;
                    }

                }
                else
                {
                    //先查询修改后变大的SORTID有没有越界
                    int MaxSortid =Convert.ToInt32(DbHelperSQL.GetSingle("select top 1 SortID from " + Table + " where " + where + " Order by SortID desc"));
                    if (SortIDEnd > MaxSortid)
                    {
                        SortIDEnd = MaxSortid;
                    }
                    if (string.IsNullOrEmpty(where))
                    {
                        where = " 1=1";
                    }
                    else
                    {
                        sql = "UPDATE " + Table + " SET SORTID = SORTID- 1 where " + where + " and  SORTID BETWEEN " + SortIDBefore + " AND " + SortIDEnd;
                    }

                }
                DbHelperSQL.ExecuteSql(sql);
                int ReturnSortID = SortIDEnd;

                return ReturnSortID;
          
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Table">表名</param>
        /// <param name="PrimaryKey">表中的主键</param>
        /// <param name="OriSortid">原来的排序id</param>
        public void Reorder(BaseDataAccess DBClass,string Table, string PrimaryKey,string where)
        {
            // Common.WriteOperation.WriteEnd("select " + PrimaryKey + ",SortId FROM " + Table + " order by SortID ASC", true);
           
                DataTable dt = new DataTable();
                if (string.IsNullOrEmpty(where))
                {
                    dt = DBClass.GetDataTable("select " + PrimaryKey + ",SortId FROM " + Table + " order by SortID ASC");
                }
                else
                {
                    dt = DBClass.GetDataTable("select " + PrimaryKey + ",SortId FROM " + Table + " where " + where + " order by SortID ASC");
                }


                for (int a = 1; a <= dt.Rows.Count; a++)
                {
                    int temp = a - 1;
                    DBClass.ExecuteNonQuery("UPDATE " + Table + " SET SORTID =" + temp + " WHERE " + PrimaryKey + " = " + dt.Rows[temp][PrimaryKey].ToString());
                }
               
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Table">表名</param>
        /// <param name="PrimaryKey">表中的主键</param>
        /// <param name="OriSortid">原来的排序id</param>
        public void DReorder(BaseDataAccess DBClass,string sql, string Table, string PrimaryKey)
        {
            // Common.WriteOperation.WriteEnd("select " + PrimaryKey + ",SortId FROM " + Table + " order by SortID ASC", true);
            
                DataTable dt = DBClass.GetDataTable(sql);
                for (int a = 1; a <= dt.Rows.Count; a++)
                {
                    int temp = a - 1;
                    DBClass.ExecuteNonQuery("UPDATE " + Table + " SET SORTID =" + a + " WHERE " + PrimaryKey + " = " + dt.Rows[temp][PrimaryKey].ToString());
                }
               
        }

    }
}
